using SharpDX;
using SharpDX.Direct2D1;
using DW = SharpDX.DirectWrite;

namespace FadeGameFramework
{
    public class RenderTargetDx : IRenderTarget
    {
        private readonly RenderTarget target;

        public RenderTargetDx(RenderTarget target) {
            this.target = target;
        }

        public void Clear(Color color) {
            target.Clear(color);
        }

        public void DrawText(string text, DW.TextFormat textFormat, RectangleF rect, Brush brush) {
            target.DrawText(text, textFormat, rect, brush);
        }

        public void DrawText(string text, string fontName, float fontSize,
            Color color, Vector2 position, float width = 100, float height = 100) {
            var factory = new DW.Factory(DW.FactoryType.Shared);
            var brush = new SolidColorBrush(target, color);
            var textFormat = new DW.TextFormat(factory, "Microsoft Yahei Mono", fontSize);
            DrawText(text, textFormat, new RectangleF(position.X, position.Y, width, height), brush);
        }

        public void DrawBitmap(Bitmap bitmap, Size2F frameSize,
            Vector2 position, RectangleF srcRectangleF, float opacity = 1f) {
            target.DrawBitmap(bitmap,
                new RectangleF(position.X, position.Y,
                    frameSize.Width,
                    frameSize.Height),
                opacity,
                BitmapInterpolationMode.NearestNeighbor,
                srcRectangleF
                );
        }

        public void DrawBitmap(Bitmap bitmap, Vector2 position, float opacity = 1f) {
            var width = bitmap.Size.Width;
            var height = bitmap.Size.Height;
            target.DrawBitmap(bitmap,
                new RectangleF(position.X, position.Y,
                    bitmap.Size.Width,
                    bitmap.Size.Height),
                opacity,
                BitmapInterpolationMode.NearestNeighbor,
                new RectangleF(0, 0, width, height)
                );
        }

        public void DrawSprite(Sprite sprite, float opacity = 1f) {
            target.DrawBitmap(sprite.Texture,
                new RectangleF(sprite.PositionX, sprite.PositionY,
                    sprite.FrameSize.Width,
                    sprite.FrameSize.Height),
                opacity,
                BitmapInterpolationMode.NearestNeighbor,
                sprite.TrimAreaList[sprite.FrameIndex]
                );
        }
    }
}